﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace msBsc.Cls
{
    public class CommValiCollection
    {
        static CommValiCollection()
        {

        }

        /// <summary>
        /// 验证是否为数字
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumber(string input)
        {
            return new Regex(@"(^[0-9])|(^[+]?\d+[.]?\d)$").IsMatch(input);
        }

        /// <summary>
        /// 验证日期
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsDate(string input)
        {
            return new Regex(@"(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(10|12|0?[13578])([-\/\._])(3[01]|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(11|0?[469])([-\/\._])(30|[12][0-9]|0?[1-9])$)|(^((1[8-9]\d{2})|([2-9]\d{3}))([-\/\._])(0?2)([-\/\._])(2[0-8]|1[0-9]|0?[1-9])$)|(^([2468][048]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([3579][26]00)([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][0][48])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][2468][048])([-\/\._])(0?2)([-\/\._])(29)$)|(^([1][89][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)|(^([2-9][0-9][13579][26])([-\/\._])(0?2)([-\/\._])(29)$)").IsMatch(input);
        }


        public static bool IsDateWithTime(string input)
        {
            return new Regex(@"^[0-9]{4}[-\/\._](((0[13578]|(10|12))[-\/\._](0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)[-\/\._](0[1-9]|[1-2][0-9]|30)))[ \s]+([0-1]?[0-9]|2[0-3])[:]([0-5][0-9])$").IsMatch(input);
        }

        /// <summary>
        /// 验证密码
        /// </summary>
        /// <param name="input"></param>
        /// <returns>正确格式：以字母或数字开头，长度在6-18之间，只能包含字符、数字和下划线。 </returns>
        public static bool IsPassword(string input)
        {
            return new Regex(@"^[a-zA-Z1-9]\w{5,17}$").IsMatch(input);
        }

        /// <summary>
        /// 验证手机号码
        /// </summary>
        /// <param name="input"></param>
        /// <returns>可验证手机号和固定号码</returns>
        public static bool IsMobile(string input)
        {
            return new Regex(@"^([1]+[3,5,8]+\d{9})$").IsMatch(input);
        }

        /// <summary>
        /// 验证手机号码
        /// </summary>
        /// <param name="input"></param>
        /// <returns>可验证手机号和固定号码</returns>
        public static bool IsTell(string input)
        {
            return new Regex(@"^((\d{3,4}-)?\d{6,8})$").IsMatch(input);
        }


        /// <summary>
        /// 验证邮箱
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsEmail(string input)
        {
            return new Regex(@"^([a-zA-Z0-9]+[_/.-]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_/.-]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$").IsMatch(input);
        }

        /// <summary>
        /// 验证身份证
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsIDCard(string input)
        {
            return new Regex(@"^([\d]{15}|[\d]{18}|[\d]{17}[x|X])$").IsMatch(input);
        }

        /// <summary>
        /// 验证邮编
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsPostID(string input)
        {
            return new Regex(@"^\d{6}$").IsMatch(input);
        }

        /// <summary>
        /// 验证QQ
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsQQ(string input)
        {
            return new Regex(@"^[1-9][0-9]{4,12}$").IsMatch(input);
        }

        /// <summary>
        /// 验证IP
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsIp(string input)
        {
            return new Regex(@"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$").IsMatch(input);
        }

        /// <summary>
        /// 验证guid类型
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsGuid(string input)
        {
            return new Regex(@"^[a-fA-F0-9]{8}-([a-fA-F0-9]{4}-){3}[a-fA-F0-9]{12}$").IsMatch(input);
        }

        /// <summary>
        /// 判断字符串的字节长度
        /// </summary>
        /// <param name="len">字符串</param>
        /// <returns>长度</returns>
        public static int GetStrLength(string len)
        {
            try
            {
                Regex re = new Regex(@"[\u2E80-\u9FFF]");
                int strLen = 0;
                char[] stringChar = len.ToCharArray();

                foreach (char item in stringChar)
                {
                    if (re.IsMatch(item.ToString()))
                    {
                        strLen += 2;
                    }
                    else
                    {
                        strLen += 1;
                    }
                }
                return strLen;
            }
            catch (Exception)
            {
                return 999999;
            }
        }


        #region 获取用户IP
        /// <summary>
        /// 获取用户登录IP
        /// </summary>
        /// <returns>返回用户登录IP</returns>
        public static string GetIp()
        {
            //判断是否使用了代理
            string ipString = HttpContext.Current.Request.ServerVariables["HTTP_VIA"] == null
                ? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]
                : HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            //判断获取的值是否为IPV6
            ipString = ipString == "::1" ? "127.0.0.1" : ipString;

            //返回IP
            return ipString;
        }
        #endregion

    }
}
